﻿using DevExpress.Pdf.Native.BouncyCastle.Asn1.Ocsp;
using DevExpress.XtraEditors;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinformDevelopFrame.Common;
using WinformDevelopFrame.Core.Extend;
using WinformDevelopFrame.Core.Form;
using WinformDevelopFrame.Entity;
using WinformDevelopFrame.IServices;
using WinformDevelopFrame.Services;

namespace WinformDevelopFrame.UI
{
    public partial class FrmUserStatus : BaseForm
    {
        private IsysuserService _sysuserService;
        private IsysmenuService _sysmenuService;
        public FrmUserStatus(IsysuserService sysuserService, IsysmenuService sysmenuService)
        {
            _sysuserService = sysuserService;
            _sysmenuService= sysmenuService;
            InitializeComponent();
            Init();
        }

        public override void Form_Load()
        {
            //xtraTabPage2.Visible = false;
            xtraTabControl1.TabPages.Remove(xtraTabPage2);
        }

        public override void SetToolBtnFunction(SimpleButton btnLogout)
        {
            if (btnLogout.Name == "btnLogout")
            {
                btnLogout.Click += BtnLogout_Click;
            }
        }

        private void BtnLogout_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            var user = (sysuser)gridView1.GetRow(gridView1.FocusedRowHandle);
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(ConfigurationManager.AppSettings["Redis"]);
            ISubscriber publisher = redis.GetSubscriber();
            string channel = "Channel1";
            string message = user.Id.ToString();
            publisher.Publish(channel, message);
            user.LoginStatus = LoginStatus.Notified;
            _sysuserService.UpdateN(user);
            SetGridData();
        }

        public override void SetColumns(Dictionary<string, string> Field_ColumnName)
        {
            base.SetColumns(Field_ColumnName);
        }

        public override void AddFunction()
        {
            base.AddFunction();
        }

        public override void EditFunction()
        {
            base.EditFunction();
        }

        public override bool SaveFunction()
        {
            return base.SaveFunction();
        }

        public override bool DeleteFunction()
        {
            return base.DeleteFunction();
        }

        public override bool CancelFunction()
        {
            return base.CancelFunction();
        }

        public override void GridControlDoubleClickFunction()
        {
            xtraTabControl1.SelectedTabPageIndex = 0;
            base.GridControlDoubleClickFunction();
        }

        public override void CloseFunction()
        {
            base.CloseFunction();
        }

        public override void InitToolBtnStatusFunction(FormStatus status, SimpleButton btn)
        {
            if (btn.Name == "btnLogout")
            {
                btn.Enabled = true;
            }
        }

        /// <summary>
        /// 设置数据 设置列名
        /// </summary>
        private void Init()
        {
            fileds.Add("UserName", "用户名");
            fileds.Add("LoginAccount", "登陆账号");
            fileds.Add("Password", "密码");
            fileds.Add("Iphone", "电话");
            fileds.Add("Address", "地址");
            fileds.Add("Status", "状态");
            fileds.Add("Sex", "性别");
            fileds.Add("Email", "邮箱");
            fileds.Add("LoginLastTime", "最后登录时间"); 
            fileds.Add("IP", "登录IP"); 
            fileds.Add("RoleId", "角色");
            fileds.Add("LoginStatus", "登录状态");
            var m = _sysmenuService.QueryByClause(p => p.MenuCode.Equals(this.Name));
            //获取工具按钮
            var menus = _sysmenuService.QueryListByClause(p => p.PId == m.Id).OrderBy(p => p.OrderNo).ToList();
            menus.ForEach(p =>
            {
                btns.Add(new SimpleButton()
                {
                    Text = p.MenuText,
                    Name = p.MenuCode,
                    Image = Image.FromFile(Application.StartupPath + p.MenuIcon)
                });
            });
           
        }
        public override void SetGridData()
        {
            gridControl1.DataSource = _sysuserService.QueryListByClause(p=>p.LoginStatus=="在线");
        }
    }
}
